//
//  Mine_ViewController.m
//  duoduo
//
//  Created by wanghao on 16/10/11.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "Mine_ViewController.h"
#import "BAXDetailVC.h"
#import "BAXDetailVC.h"
#import "DrivingOrderVC.h"
#import "MyMoneyViewController.h"
#import "SettingViewController.h"
#import "IntegralViewController.h"
#import "CouponViewController.h"
#import "MineInfoViewController.h"
#import "HYPersonModel.h"
#import "LoginViewController.h"

@interface Mine_ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray * imageArr1;
    NSArray * imageArr2;
    NSArray * imageArr3;
    NSArray * titleArr1;
    NSArray * titleArr2;
    NSArray * titleArr3;
}
@property (nonatomic,strong) UITableView * listTableView;

@end

@implementation Mine_ViewController
{
    UIImageView *_headImageView;
    UILabel *_nameLabel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    imageArr1 = @[@"wode-pinche",@"wode-zijia"];
    imageArr2 = @[@"wode-qianbao",@"wode-jifen",@"wode-youhuiquan"];
    imageArr3 = @[@"wode-shezhi",@"shiyong"];
    titleArr1 = @[@"拼车订单",@"自驾车订单"];
    titleArr2 = @[@"我的余额",@"我的积分",@"优惠券"];
    titleArr3 = @[@"设置",@"使用说明"];
    self.title = @"我的";
    [self initUI];

}
- (void)viewWillAppear:(BOOL)animated
{
    [self initNav];
    [self makeHeadViewUI];
}
- (void)initNav
{
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden=NO;
}
- (void)makeHeadViewUI
{
    if (appD.userId) {
        HYPersonModel *model = appD.userModel;
        _nameLabel.text = model.username;
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:model.face] placeholderImage:NoHeadImage];
    }else{
        _nameLabel.text = @"点击头像登录";
    }
}
- (void)initUI
{
    _listTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, ScreenHeight-5) style:UITableViewStylePlain];
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    _listTableView.backgroundColor = LXHexColor(0xF5F6F7);
    [self.view  addSubview:_listTableView];
    _listTableView.bounces = YES;
    _listTableView.bounces = NO;
    [self initTableViewHeaderView];
    [self initTableViewFooterView];
}
- (void)initTableViewHeaderView
{
    UIImageView * headerImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth, ScreenHeight/3)];
    headerImage.image = [UIImage imageNamed:@"wode-bg"];
    UIImageView * touxiangImage = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-(ScreenWidth/4)/2, 50, ScreenWidth/4, ScreenWidth/4)];
    touxiangImage.image = [UIImage imageNamed:@"touxinag1"];
    [headerImage addSubview:touxiangImage];
    touxiangImage.layer.masksToBounds = YES;
    touxiangImage.layer.cornerRadius = ScreenWidth/8;
    _headImageView = touxiangImage;

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2-100, touxiangImage.frame.size.height+70,200,25)];
    label.text = @"执着的人儿";
    _nameLabel = label;

    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    [headerImage addSubview:label];
    _listTableView.tableHeaderView = headerImage;

    headerImage.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toInfoClick)];
    [headerImage addGestureRecognizer:tap];
}

- (void)toInfoClick
{
    if(appD.userId){
        MineInfoViewController * info = [[MineInfoViewController alloc]init];
        [self.navigationController pushViewController:info animated:YES];
    }else{
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}
- (void)initTableViewFooterView
{
    UIButton * button = [[UIButton alloc]init];
    button.frame = CGRectMake(20, 20, ScreenWidth-40,45);
    button.backgroundColor = LXHexColor(0x5DCA68);
    button.titleLabel.font = [UIFont systemFontOfSize:18];
    [button setTitle:@" 一键联系客服" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"dianhua"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"dianhua"] forState:UIControlStateSelected];
    button.layer.cornerRadius = 6;
    button.layer.masksToBounds = YES;
    UIView * myview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 70)];
    myview.backgroundColor = [UIColor clearColor];
    [myview addSubview:button];
    _listTableView.tableFooterView = myview;
}
//进入支付
- (void)buyClick
{

}
#pragma mark --- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else if (section==1){
        return 3;
    }
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"listcell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"listcell"];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    if (indexPath.section == 0) {
        cell.imageView.image = [UIImage imageNamed:imageArr1[indexPath.row]];
        cell.textLabel.text = titleArr1[indexPath.row];
    }else if (indexPath.section == 1){
        cell.imageView.image = [UIImage imageNamed:imageArr2[indexPath.row]];
        cell.textLabel.text = titleArr2[indexPath.row];
    }else {
        cell.imageView.image = [UIImage imageNamed:imageArr3[indexPath.row]];
    cell.textLabel.text = titleArr3[indexPath.row];


    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.listTableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            BAXDetailVC * order = [[BAXDetailVC alloc]init];
            [self.navigationController pushViewController:order animated:YES];
        }
        if (indexPath.row == 1) {
            DrivingOrderVC * driving = [[DrivingOrderVC alloc]init];
            [self.navigationController pushViewController:driving animated:YES];
        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            MyMoneyViewController * money = [[MyMoneyViewController alloc]init];
            [self.navigationController pushViewController:money animated:YES];
        }
        if (indexPath.row == 1) {
            IntegralViewController * intergra = [[IntegralViewController alloc]init];
            [self.navigationController pushViewController:intergra animated:YES];
        }
        if (indexPath.row == 2) {
            CouponViewController * coupon = [[CouponViewController alloc]init];
            [self.navigationController pushViewController:coupon animated:YES];
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            SettingViewController * setting = [[SettingViewController alloc]init];
            [self.navigationController pushViewController:setting animated:YES];
        }

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
@end
